package com.dries.portal.module.convener.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dries.portal.module.convener.dto.ConvenerLikesParams;
import com.dries.portal.module.convener.entity.ConvenerLikes;
import com.dries.portal.module.convener.mapper.ConvenerLikesMapper;
import com.dries.portal.module.convener.service.IConvenerLikesService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
public class ConvenerLikesServiceImpl extends ServiceImpl<ConvenerLikesMapper, ConvenerLikes> implements IConvenerLikesService {


    @Override
    public boolean likes(ConvenerLikesParams params) {

        ConvenerLikes likes = baseMapper.selectOne(new QueryWrapper<ConvenerLikes>().lambda()
                .eq(ConvenerLikes::getConvenerId, params.getConvenerId())
                .eq(ConvenerLikes::getUserId, params.getUserId())
        );

        if (likes != null) {
            return true;
        } else {
            ConvenerLikes like = new ConvenerLikes();
            BeanUtils.copyProperties(params, like);
            return baseMapper.insert(like) == 1;
        }

    }

    @Override
    public boolean dislikes(ConvenerLikesParams params) {

        ConvenerLikes likes = baseMapper.selectOne(new QueryWrapper<ConvenerLikes>().lambda()
                .eq(ConvenerLikes::getConvenerId, params.getConvenerId())
                .eq(ConvenerLikes::getUserId, params.getUserId())
        );

        if (likes == null) {
            return true;
        } else {
            int i = baseMapper.deleteById(likes);
            return  i == 1;
        }

    }
}